# -*- coding: utf8 -*-

class Cookie(object):

    """ Implementation of http://tools.ietf.org/html/rfc2109 HTTP cookies. """

    def __init__(self, name, value="", comment=None, domain=None, 
            max_age=None, path=None, secure=False):
        if domain is not None:
            assert domain.startswith("."), "domain must start with a dot"
        self.name = name
        self.value = value
        self.comment = comment
        self.domain = domain
        self.max_age = max_age
        self.path = path
        self.secure = secure

    def delete(self):
        self.max_age = 0

    def __str__(self):
        result = ["{name}={value};Version=1"
            .format(name=self.name, value=self.value)]
        if self.comment:
            result.append("Comment={}".format(self.comment))
        if self.domain:
            result.append("Domain={}".format(self.domain))
        if self.max_age:
            result.append("Max-Age={}".format(self.max_age))
        if self.path:
            result.append("Path={}".format(self.path))
        if self.secure:
            result.append("Secure")
        return ";".join(result)
        
